package com.andnnl.mongo;

import cn.hutool.json.JSONUtil;
import com.andnnl.mongo2.Mobile2DAO;
import com.andnnl.mongo2.Mobile2Entity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by chenss on 2020/11/4.
 */
public class MongoTest {
    private static final Logger logger = LoggerFactory.getLogger(MongoTest.class);
    static AtomicInteger atomicInteger = new AtomicInteger();

    public static void main(String[] args) throws Exception {
//        testinsert();
        testinsert2();//直接以号码为id

//        testQuery();
//        testQueryList();
//        testThread();
//        testThread2();

    }

    public static void testThread() throws InterruptedException, ExecutionException, IOException {
//        IRedisService service = getService();
        MobileDAO user = new MobileDAO();
        long startMobile = 13801010001L;
        long t1 = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(200);
        CompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);
        int tasks = 200;
        for (int i = 0; i < tasks; i++) {
            int finalI = i;
//            completionService.submit(() -> System.out.println("asdf" + finalI), "aa" + finalI);
            int finalI1 = i;
            completionService.submit(() -> {
                long ts1 = System.currentTimeMillis();

//            executorService.submit(() -> {
                for (int k = 0; k < 500; k++) {
                    //返回Null,表示操作失败
                    MobileEntity e = user.findByMobile(startMobile + finalI1 * 10000 + k);
                    int a = atomicInteger.incrementAndGet();
//                    if(a%1000==0){
//                        logger.info("count:" + a);
//                    }
//                    if (rsStr == null)
//                        logger.info("" + finalI);
                }
                long ts2 = System.currentTimeMillis();
                logger.info("finish aa " + finalI + " : " + (ts2 - ts1));
            }, "aa" + finalI);
        }
        for (int i = 0; i < tasks; i++) {
            String str = completionService.take().get();//先完成的结果先执行后续处理
            //do something
            logger.info("finish->" + str);
        }
//        completionService.take().isDone();

        //汇总操作
        executorService.shutdown();
        long t2 = System.currentTimeMillis();
        logger.info("finish time:{}", t2 - t1);
        System.in.read();

    }

    public static void testThread2() throws InterruptedException, ExecutionException, IOException {
//        IRedisService service = getService();
        Random random = new Random();
        MobileDAO user = new MobileDAO();
        long startMobile = 13801010001L;
//        long startMobile = 14801010001L;
        long t1 = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(200);
        CompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);
        int tasks = 2000;
        for (int i = 0; i < tasks; i++) {
            int finalI = i;
            int finalI1 = i;
            completionService.submit(() -> {
                long ts1 = System.currentTimeMillis();
                List<Long> list = new ArrayList<>();
                for (int k = 0; k < 500; k++) {
                    list.add(startMobile + random.nextInt(10000000));
                }
                List<MobileEntity> rsList = user.findInList(list);
                System.out.println("rsList.size():" + rsList.size());
                long ts2 = System.currentTimeMillis();
                logger.info("finish aa " + finalI + " : " + (ts2 - ts1));
            }, "aa" + finalI);
        }
        for (int i = 0; i < tasks; i++) {
            String str = completionService.take().get();//先完成的结果先执行后续处理
            //do something
            logger.info("finish->" + str);
        }
//        completionService.take().isDone();

        //汇总操作
        executorService.shutdown();
        long t2 = System.currentTimeMillis();
        logger.info("finish time:{}", t2 - t1);
        System.in.read();

    }

    private static void testQuery() {
        MobileDAO user = new MobileDAO();
        long a = 13801010001L;

        MobileEntity ss = user.findByMobile(a + 1);
        System.out.println(ss.getMobile());
        long t1 = System.currentTimeMillis();
        for (int i = 0; i < 1; i++) {
            MobileEntity e = user.findByMobile(a + i);
//            System.out.println(e.getMobile());
        }
        long t2 = System.currentTimeMillis();

        System.out.println((t2 - t1));
    }


    private static void testQueryList() {
        MobileDAO user = new MobileDAO();
        long a = 13801010001L;
        Random random = new Random();
        MobileEntity ss = user.findByMobile(a + 1);
        System.out.println(ss.getMobile());
        long t1 = System.currentTimeMillis();
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            list.add(a + random.nextInt(10000000));
        }
        List<MobileEntity> rsList = user.findInList(list);
        System.out.println("rsList.size():" + rsList.size());
//        logger.info(JSONUtil.toJsonStr(rsStrList));
        System.out.println("rsList.size():" + JSONUtil.toJsonStr(rsList));
        long t2 = System.currentTimeMillis();

        System.out.println((t2 - t1));
    }

    private static void testinsert() {
        MobileDAO user = new MobileDAO();
        long a = 13800010001L;
        List<MobileEntity> list = new ArrayList<>();
        for (int i = 0; i < 100000000; i++) {
            MobileEntity u = new MobileEntity();
            u.setMobile(a + i);
            u.setType(11);
            list.add(u);
            if (list.size() >= 1000) {
                user.insert(list);
                list.clear();
            }
        }
        if (list.size() >= 1000) {
            user.insert(list);
            list.clear();
        }
    }

    private static void testinsert2() {
        Mobile2DAO user = new Mobile2DAO();
        long a = 13800010001L;
        List<Mobile2Entity> list = new ArrayList<>();
        for (int i = 0; i < 100000000; i++) {
            Mobile2Entity u = new Mobile2Entity();
            u.setMobile(a + i);
            u.setType(11);
            list.add(u);
            if (list.size() >= 1000) {
                user.insert(list);
                list.clear();
            }
        }
        if (list.size() >= 1000) {
            user.insert(list);
            list.clear();
        }
    }

}
